<template>
  <div class="articles-container">
    <!-- 页面整体 -->
    <el-card shadow="never" label-width="80px">
      <!-- 头部组件 -->
      <el-row>
        <el-col :span="18">
          <el-form inline size="small">
            <el-form-item label="关键字" style="margin-left: 25px">
              <el-input v-model="reqParams.keyword"></el-input>
            </el-form-item>
            <el-form-item label="状态" style="margin-left: 45px">
              <el-select placeholder="请选择" v-model="reqParams.state">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="deleteContent()">清除</el-button>
              <el-button @click="searchContent()" type="primary"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            style="float: right"
            @click="addContent()"
          >
            新增技巧
          </el-button>
        </el-col>
      </el-row>
      <!-- 提示信息 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 20px"
      >
      </el-alert>
      <!-- 表格展示 -->
      <el-table :data="articles" style="width: 100%">
        <el-table-column label="序号" width="80" type="index"></el-table-column>
        <el-table-column label="文章标题" width="500px">
          <template slot-scope="scope">
            {{ scope.row.title }}
            <a
              href="#"
              class="el-icon-film"
              v-if="scope.row.videoURL"
              @click.prevent="playVideo(scope.row)"
            >
            </a>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"></el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">
            {{ scope.row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100px">
          <template slot-scope="scope">
            {{ scope.row.state === 1 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="previewArticle(scope.row)"
              >预览</el-button
            >
            <el-button type="text" @click="useArticle(scope.row)">{{
              scope.row.state === 1 ? '禁用' : '启用'
            }}</el-button>
            <el-button
              type="text"
              @click="addContent(scope.row)"
              :disabled="scope.row.state === 1"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="deleteArticle(scope.row)"
              :disabled="scope.row.state === 1"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部组件 -->
      <el-pagination
        class="pager"
        style="margin-top: 20px; text-align: right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :current-page="reqParams.page"
        :page-size="reqParams.pagesize"
        @current-change="changePager"
        @size-change="changeSize"
        :page-sizes="[5, 10, 20, 50]"
      >
      </el-pagination>
      <!-- 播放器弹框 -->
      <!-- <span class="quitVideo" @click="quitVideo" v-if="show">x</span> -->
      <el-dialog
        title
        :visible.sync="dialogPlay"
        width="30%"
        class="one"
        @close="close"
      >
        <video
          class="video"
          :src="videoURL"
          ref="dialogVideo"
          width="100%"
          controls
          autoplay
          loop
        >
          <meta name="referrer" content="never" />
        </video>
      </el-dialog>
      <!-- 文章预览 -->
      <articles-preview
        ref="articlesPreview"
        :data="currArticle"
      ></articles-preview>
      <!-- 文章新增 -->
      <articles-add
        ref="articlesAdd"
        :data="currArticle"
        @updateList="updateList()"
      ></articles-add>
    </el-card>
  </div>
</template>

<script>
import {
  list as articlesList,
  remove,
  changeState
} from '@/api/hmmm/articles.js'
import ArticlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'
export default {
  name: 'ArticlesList',
  components: { ArticlesAdd, ArticlesPreview },
  data () {
    return {
      dialogPlay: false,
      // api请求的数据
      reqParams: {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      // 文章列表
      articles: [],
      // 文章总数
      total: 0,
      videoURL: null,
      currArticle: {},
      show: false
    }
  },
  created () {
    this.getArticlesList()
  },

  methods: {
    // 获取文章列表
    async getArticlesList () {
      console.log(this.articles)
      const { data } = await articlesList(this.reqParams)
      this.articles = data.items
      this.total = data.counts
    },
    // 删除按钮
    async deleteArticle (article) {
      await this.$confirm('此操作将永久删除该文章,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(article)
      this.$message.success('删除成功!')
      this.getArticlesList()
    },
    // 切换状态
    async useArticle (article) {
      await changeState({
        id: article.id,
        state: article.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      article.state = article.state === 1 ? 0 : 1
    },
    // 新增修改文章
    addContent (article = {}) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesAdd.open()
      })
    },
    // 新增修改后立即渲染
    updateList () {
      this.getArticlesList()
    },
    close () {
      this.$refs.dialogVideo.pause()
    },
    // 分页
    changePager (newPage) {
      this.reqParams.page = newPage
      this.getArticlesList()
    },
    // 尺码
    changeSize (newSize) {
      this.reqParams.page = 1
      this.reqParams.pagesize = newSize
      this.getArticlesList()
    },
    // 清除关键字
    deleteContent () {
      this.reqParams = {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      }
      this.getArticlesList()
    },
    // 筛选状态
    searchContent () {
      this.reqParams.page = 1
      this.getArticlesList()
    },
    // 播放器弹出
    playVideo (row) {
      this.dialogPlay = true
      this.videoURL = row.videoURL
      this.show = true
    },
    // 播放器关闭
    // quitVideo () {
    //   console.log('关闭了')
    //   this.dialogPlay = false
    //   this.show = false
    // },
    // 预览文章按钮
    previewArticle (article) {
      this.currArticle = article
      console.log(this.currArticle)
      this.$nextTick(() => {
        this.$refs.articlesPreview.open()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.el-icon-film {
  color: indigo;
}
// .quitVideo {
//   background-color: #000;
//   width: 200px;
//   height: 200px;
//   border-radius: 20%;
//   z-index: 9999;
// }
::v-deep .one .el-dialog__header {
  display: none;
}
::v-deep .one .el-dialog__body {
  padding: 0px;
  margin-top: 300px;
}

.articles-container {
  padding: 10px;
  .el-icon-film {
    color: blue;
    font-size: 18px;
  }
}
.video-preview {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .close {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
